from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
       #镜头跟进效果 
        self.camera.frame.save_state()
         #题目出现
        text = Tex(r"\text{对于未知参数a和b,反常积分}$\int _{1}^{+\infty }\left[ \dfrac{a-2e+2}{2}+\dfrac{a\left( 1-2x+ex\right) -\dfrac{1}{2}a^{2}x+bx}{2x^{2}+ax}\right] dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0)
        text1 = Tex(
                   r"\text{(A)一定不收敛}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-8,0])
        text2 = Tex(
                    r"\text{(B)一定收敛}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-8,0])
        text3 = Tex(
                   r"\text{(C)若收敛,则其值为1.}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-14,0])
        text4 = Tex(
                    r"\text{(D)若收敛,则其值为e.}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-14,0])

        self.play(Write(text),self.camera.frame.animate.scale(0.8),Write(text1),Write(text2),Write(text3),Write(text4))
        self.wait(0.1)
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([0,20,0]),text1.animate().move_to([-22,12,0]) ,text2.animate().move_to([22,12,0]),text3.animate().move_to([-22,6,0]),text4.animate().move_to([22,6,0]) )

        #半透明矩阵扩展
        rectangle=Rectangle(color=BLUE,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-22,12,0])
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([22,12,0])
        rectangle2=Rectangle(color=YELLOW,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-22,6,0])
        rectangle3=Rectangle(color=YELLOW,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([22,6,0])
        self.play(
            rectangle.animate.stretch(30.0,dim=0),
            rectangle1.animate.stretch(30.0,dim=0)
        )
        self.wait(0.5)

        self.play(
            rectangle2.animate.stretch(30.0,dim=0),
            rectangle3.animate.stretch(30.0,dim=0)
        )
        self.wait(0.5)

        rectangle4=Rectangle(color=BLUE,fill_opacity=0.0,width=61.5,height=12
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle4.move_to([18, 20, 0])
        rectangle5=Rectangle(color=BLUE,fill_opacity=0.0,width=61.5,height=12
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle5.move_to([18, 20, 0])
        self.play(Create(rectangle4),Create(rectangle5))
        #框框转化为两积分

        text5 = Tex(
                   r"$\int _{1}^{+\infty }\dfrac{a-2e+2}{2}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-3,0])
        text6 = Tex(
                    r"$\int _{1}^{+\infty }\dfrac{a\left( 1-2x+ex\right) -\dfrac{1}{2}a^{2}x+bx}{2x^{2}+ax}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-3,0])


        self.play(
            ReplacementTransform(rectangle4,text5),
            ReplacementTransform(rectangle5,text6)
        )
        self.wait(0.5)
        text7 = Tex(
                    r"\text{当}$\dfrac{a-2e+2}{2}=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-38,-10,0])
        text8 = Tex(
                    r"\text{收}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-10,0])
        text9 = Tex(
                    r"\text{当}$\dfrac{a-2e+2}{2}\neq 0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-38,-17,0])
        text10 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-17,0])
        self.play(Write(text7))
        self.play(Write(text8))
        self.play(Write(text9))
        self.play(Write(text10))
        self.wait(0.5)

        text11 = Tex(
                    r"$\int ^{+\infty }_{1}\dfrac{\left( b-\dfrac{1}{2}a^{2}-2a+ea\right) x+a}{2x^{2}+ax}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-3,0])

        self.play(
            ReplacementTransform(text6,text11)
        )
        self.wait(0.5)


        text17 = Tex(
                    r"\text{当}$b-\dfrac{1}{2}a^{2}-2a+ea=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([2,-10,0])
        text18 = Tex(
                    r"\text{收}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-10,0])
        text19 = Tex(
                    r"\text{当}$b-\dfrac{1}{2}a^{2}-2a+ea\neq 0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([2,-17,0])
        text20 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-17,0])
        self.play(Write(text17))
        self.play(Write(text18))
        self.play(Write(text19))
        self.play(Write(text20))
        self.wait(0.5)

        rectangle6=Rectangle(color=GREEN,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle6.move_to([-22,-10,0])
        rectangle7=Rectangle(color=GREEN,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle7.move_to([22,-10,0])
        self.play(Create(rectangle6),Create(rectangle7))

        rectangle10=Rectangle(color=GREEN,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle10.move_to([-22,-10,0])
        rectangle11=Rectangle(color=BLUE,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle11.move_to([22,-10,0])


        rectangle8=Rectangle(color=GREEN,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle8.move_to([-22,-17,0])
        rectangle9=Rectangle(color=BLUE,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle9.move_to([22,-17,0])
        rectangle12=Rectangle(color=BLUE,fill_opacity=0.0,width=5,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle12.move_to([-22,-17,0])
        self.play(Create(rectangle8),Create(rectangle9)
                  ,ReplacementTransform(rectangle6,rectangle10)
                  ,ReplacementTransform(rectangle7,rectangle11))
        self.wait(0.5)
        self.play(FadeOut(rectangle10),FadeOut(rectangle11),Create(rectangle12))
        self.play(FadeOut(rectangle8),FadeOut(rectangle9),FadeOut(rectangle12), 
                  rectangle.animate.set_color(RED),rectangle1.animate.set_color(RED)
                  )
        
        text21 = Tex(
                    r"$\int ^{+\infty }_{1}\dfrac{a}{2x^{2}+ax}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(
            ReplacementTransform(text5,text21),
            ReplacementTransform(text11,text21),
            ReplacementTransform(text7,text21),
            ReplacementTransform(text8,text21),
            ReplacementTransform(text9,text21),
            ReplacementTransform(text10,text21),
            ReplacementTransform(text17,text21),
            ReplacementTransform(text18,text21),
            ReplacementTransform(text19,text21),
            ReplacementTransform(text20,text21)

        )
        self.wait(0.5)
        text22 = Tex(
                    r"$\int \dfrac{a}{x\left( 2x+a\right) }dx$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text23 = Tex(
                    r"$\int \dfrac{2a}{2x\left( 2x+a\right) }dx$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text24 = Tex(
                    r"$\int \dfrac{a}{2x\left( 2x+a\right) }d2x$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text25 = Tex(
                    r"$\int \left( \dfrac{1}{2x}-\dfrac{1}{2x+a}\right) dx$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text26 = Tex(
                    r"$\ln \dfrac{2x}{2x+a}+c$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text27 = Tex(
                    r"$=1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(Write(text22))
        self.play(
            ReplacementTransform(text22,text23)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text23,text24)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text24,text25)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text25,text26)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text26,text27)
        )
        self.wait(0.5)
        self.play(
                  rectangle2.animate.set_color(GREEN),rectangle3.animate.set_color(RED)
                  )
        self.wait()
